package at.guigu.controller;

import at.guigu.exception.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ProjectEcepAdvice {
    // 捕获所有异常进行处理
    @ExceptionHandler(Exception.class)
    public ModelAndView doException(Exception e){
        ModelAndView modelAndView = new ModelAndView();
        // 若是ClassCastException异常则映射到errorShow1.jsp页面
        if (e instanceof ClassCastException) {
            modelAndView.setViewName("errorShow1");
            // 若是自定义MyException异常则映射到errorShow5.jsp页面
        } else if (e instanceof MyException) {
            modelAndView.setViewName("errorShow5");
            // 若是其它异常则映射到error.jsp页面
        } else {
            modelAndView.setViewName("error");
        }
        return  modelAndView;
    }
}
